<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS</title>
  <style>


  </style>
</head>

<body>
  <div class="card">
    <p>请想一个1~100之间的数字，我来猜</p>
    <p id="output"></p>
  </div>

  <button onclick="dala()">大啦</button>
  <button onclick="xiaola()">小啦</button>

</body>

<script>
  class Caishu {
    isnum = 0

    constructor(min = 1, max = 100) {
      this.min = min
      this.max = max

      this.output()
    }

    // 猜大了
    dala() {
      this.max = this.isnum
      this.output()
      return this.isnum
    }

    // 猜小了
    xiaola() {
      this.min = this.isnum
      this.output()
      return this.isnum
    }

    // 猜数
    output() {
      if (this.isnum == this.min) {
        this.isnum = Math.round(this.min + (this.max - this.min) / 2)
      } else {
        this.isnum = Math.floor(this.min + (this.max - this.min) / 2)
      }
      const outputElem = document.getElementById('output')
      outputElem.innerHTML = `是不是 ${this.isnum}`;
    }
  }

  let caishu = new Caishu

  // function dala() {caishu.dala();}
  // function xiaola() {caishu.xiaola()}

  function wocai(num = 72) {
    let count = 0
    do {
      if (caishu.isnum < num) {
        caishu.xiaola()
      } else if (caishu.isnum > num) {
        caishu.dala()
      } else if (caishu.isnum == num) {
        console.log(`猜中了是${num}`)
        break;
      }

      count++
    } while (count < 500)
  }

  // wocai(1)

  (function () {
    for (let i = 1; i <= 100; i++) {
      wocai(i)
      caishu = new Caishu
    }

  })()


</script>

</html>